/*1942 game clone
 * Authors:
 * Abdollah Tabareh
 * Bharat Bhushan Konka
 * Martin Svensson 
 * Richard Zianne
 * Tigran Harutyunyan
 * 
 * 2010
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Game1942
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Bullet : FlyingObject
    {

        public Vector2 _direction { get; set; }
        public float _bulletSpeed { get; set; }
        protected const short WIDTH = 11;
        protected const short HEIGHT = 11;
        public bool _playerBullet { get; set; }
        public Bullet(Game game, ref Texture2D texture)
            : base(game)
        {
            _power = 10;
            _bulletSpeed = 5;
            _texture = texture;
            _position = new Vector2();
            _spriteRectangle = new Rectangle(48, 213, WIDTH, HEIGHT);
            _spriteBatch =
                (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            
            
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            _position += _direction * _bulletSpeed;

            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            _spriteBatch.Draw(_texture, _position, _spriteRectangle, Color.White);
            _spriteBatch.End();
            base.Draw(gameTime);
        }

        /// <summary>
        /// launch a bullet with a specific power, direction and speed
        /// used if diferent bullits have a diferent speeds 
        /// </summary>
        /// <param name="position">the start opsision of the bullet</param>
        /// <param name="direction">The direction of the bullet</param>
        /// <param name="speed">the speed of the bulle</param>
        public void launch(Vector2 position, Vector2 direction, float speed, bool playerBullit)
        {
            _bulletSpeed = speed;
            launch(position, direction, playerBullit);
        }

        /// <summary>
        /// launch a bullet with a specific power, direction and speed
        /// used if diferent bullits have a difrent degree of efficiency
        /// and diferent speed
        /// </summary>
        /// <param name="position">the start opsision of the bullet</param>
        /// <param name="direction">The direction of the bullet</param>
        /// <param name="power">the power of the bullet</param>
        /// <param name="speed">the speed of the bullet</param>
        public void launch(Vector2 position, Vector2 direction, int power, float speed, bool playerBullit )
        {
            _bulletSpeed = speed;
            launch(position, direction, power, playerBullit);
        }
        
        /// <summary>
        /// launch a bullet with a specific power and direction,
        /// used if diferent bullits have a difrent degree of efficiency 
        /// </summary>
        /// <param name="position">the statr opsision of the bullet</param>
        /// <param name="direction">The direction of the bullet</param>
        /// <param name="power">the power of the bullet</param>
        public void launch(Vector2 position, Vector2 direction, int power, bool playerBullit)
        {
            _power = power;
            launch(position, direction, playerBullit);
        }
        /// <summary>
        /// launch a bullet
        /// </summary>
        /// <param name="position">the statr opsision of the bullet</param>
        /// <param name="direction">The direction of the bullet</param>
        public void launch(Vector2 position, Vector2 direction, bool playerBullit)
        {
            _direction = direction;
            _position = position;
            _playerBullet = playerBullit;

        }
        public bool CheckCollision(Rectangle rect)
        {
            Rectangle spriterect = new Rectangle((int)_position.X, (int)_position.Y, WIDTH, HEIGHT);
            return spriterect.Intersects(rect);

        }
        public Rectangle BoundsRect()
        {
            return new Rectangle((int)_position.X, (int)_position.Y, (int)WIDTH, (int)HEIGHT);
        }
        public void putInStartPos()
        {
            _position = new Vector2(-100, -100);
            _direction = new Vector2(0, 0);
        }
    }
}